package com.astrofizzbizz.stoneedgegui.utilities;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.StringTokenizer;

public class TreffersLock 
{
	private boolean lock = false;
	private String sessionID = "";
	private String phone = "";
	private String email = "";
	private StoneEdgeIIITunnel tunnel;
	
	public TreffersLock(StoneEdgeIIITunnel stoneEdgeIIITunnel) throws StoneEdgeIIIException 
	{
		sessionID = "";
		phone = "";
		email = "";
		
		tunnel = stoneEdgeIIITunnel;

		lock = false;
	    String[] response = {"",""};
	    try {response = tunnel.executeCommand("tx lock", 1024);}
	    catch (java.lang.NullPointerException e)
	    {
	    	throw new StoneEdgeIIIException("Null pointer Exception in Treffers Lock");
	    }

		int index = response[0].indexOf("user"); 
		if (index < 0) return;
		lock = true;
		new StringTokenizer(response[0].substring(index + 5)).nextToken();
		
		index = response[0].indexOf("phone"); 
		if (index >= 0) phone = new StringTokenizer(response[0].substring(index + 6)).nextToken();
		
		index = response[0].indexOf("email"); 
		if (index >= 0) email = new StringTokenizer(response[0].substring(index + 6)).nextToken();

		index = response[0].indexOf("comment"); 
		if (index >= 0) sessionID = new StringTokenizer(response[0].substring(index + 8)).nextToken();
	}
	public void setLock(String sessionID, String phone, String email) throws StoneEdgeIIIException 
	{
		this.sessionID = sessionID.replaceAll("[^a-zA-Z0-9]", "");
		this.phone = phone.replaceAll("[^a-zA-Z0-9]", "");
		this.email = email.replaceAll(" ", "");
		
		String command = "tx lock user=remote" + " email=" + this.email + " phone=" + this.phone + " comment=" + this.sessionID;
		tunnel.executeCommand(command, 1024);
	}
	public String makeShortUserName(String longUserName)
	{
		String shortUserName = "none";
		if (longUserName == null) return shortUserName;
		if (longUserName.length() < 1) return shortUserName;
		StringTokenizer st = new StringTokenizer(longUserName);
		String firstName = st.nextToken();
		String lastName = "";
		if (st.hasMoreTokens()) 
		{
			firstName = firstName.substring(0, 1);
			lastName = st.nextToken();
		}
		shortUserName = firstName + lastName;
		shortUserName = shortUserName.toLowerCase();
		shortUserName = shortUserName.replaceAll("[^a-zA-Z0-9]", "");
		return shortUserName;
	}
	public String getEmailName()
	{
		String name = "unknown";
		int index = email.indexOf("@"); 
		if (index > 1)
		{
			name = email.substring(0, index);
		}
		return name;
	}
	public void clearLock() throws StoneEdgeIIIException 
	{
		String command = "tx lock clear";
		tunnel.executeCommand(command, 1024);
	}
	public void writeSpyFile(String spyFileDirName, Date date) throws IOException
	{
		String delim = System.getProperty("file.separator");
		String spyFile = spyFileDirName + delim + "spyFile.html";
		File spyfile = new File(spyFile);
		spyfile.createNewFile();
		BufferedWriter bw = new BufferedWriter(new FileWriter(spyfile,true));
		@SuppressWarnings("deprecation")
		String spyOutputHtml = date.toGMTString() + " <br>";
		spyOutputHtml = spyOutputHtml + "Phone = " + getPhone() + " <br>";
		spyOutputHtml = spyOutputHtml + "Email = " + getEmail() + " <br><br>";
		bw.write(spyOutputHtml);
		bw.newLine();
	    bw.close();
	}

	public boolean isLock() {
		return lock;
	}
	public String getSessionID() {
		return sessionID;
	}

	public String getPhone() {
		return phone;
	}

	public String getEmail() {
		return email;
	}
	public static void main(String[] args) throws StoneEdgeIIIException  
	{
	    StoneEdgeIIITunnel stoneEdgeIIITunnel = new StoneEdgeIIITunnel(StoneEdgeIIITunnel.privateKey());	
	    
	    TreffersLock treffersLock = new TreffersLock(stoneEdgeIIITunnel);
	    System.out.println(treffersLock.isLock());
	    System.out.println(treffersLock.getSessionID());
	    System.out.println(treffersLock.getPhone());
	    System.out.println(treffersLock.getEmail());
	    stoneEdgeIIITunnel.disconnect();
	    System.exit(0);

	}


}
